package logic.ui.componentInfoMed
{
	import component.show.containers.UIFrame;
	import component.show.control.UISprite;
	
	import fl.controls.DataGrid;
	import fl.controls.Label;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;
	import fl.events.DataGridEvent;
	
	import flash.events.MouseEvent;
	
	import logic.def.UIURL;

	public class DataGridColsMed extends UISprite
	{
		public static const NAME : String = "DataGridCols";
		
		private var m_target : DataGrid;
		
		private var m_dgCols : DataGrid;
		
		private var m_dpCols : DataProvider;
		
		private var m_fraTitle : UIFrame;
		
		private var m_labAddCol : Label;
		private var m_labDisCol : Label;
		
		public function DataGridColsMed()
		{
			super(NAME,UIURL["DataGridCols"]);
			
			m_dgCols = collection["DgPro"];
			m_fraTitle = collection["UFTitle"];
			m_labAddCol = collection["LabAddCol"];
			m_labDisCol = collection["LabDisCol"];
			
			m_fraTitle.opaqueBackground = 0xFF9900;
			m_labAddCol.buttonMode = true;
			
			m_dpCols = new DataProvider;
			m_dgCols.dataProvider = m_dpCols;
			m_dgCols.editable = true;
			
			var dgCol : DataGridColumn = m_dgCols.columns[0];
			dgCol.dataField = "text";
			dgCol.editable = true;
			
			dgCol = m_dgCols.columns[1];
			dgCol.dataField = "resizable";
			dgCol.editable = true;
			
			dgCol = m_dgCols.columns[2];
			dgCol.dataField = "sortable";
			dgCol.editable = true;
			
			dgCol.sortable  = false;
			
			m_fraTitle.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
			m_fraTitle.addEventListener(MouseEvent.MOUSE_UP,onStopDrag);
			m_labAddCol.addEventListener(MouseEvent.CLICK,onClickAddColLab);
			m_labDisCol.addEventListener(MouseEvent.CLICK,onClickDisColLab);
			
			m_dgCols.addEventListener(DataGridEvent.ITEM_EDIT_END,onItemEditEnd);
		}
		
		public  override function destory() : void
		{
			m_fraTitle.removeEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
			m_fraTitle.removeEventListener(MouseEvent.MOUSE_UP,onStopDrag);
			m_labAddCol.removeEventListener(MouseEvent.CLICK,onClickAddColLab);
			m_labDisCol.removeEventListener(MouseEvent.CLICK,onClickDisColLab);
			
			m_dgCols.removeEventListener(DataGridEvent.ITEM_EDIT_END,onItemEditEnd);
			
			m_fraTitle = null;
		}
		
		private function onStartDrag(evt : MouseEvent) : void
		{
			this.startDrag();
		}
		
		private function onStopDrag(evt : MouseEvent) : void
		{
			this.stopDrag();
		}
		
		public function get target() : DataGrid
		{
			return m_target;
		}
		
		public function set target(value : DataGrid) : void
		{
			m_target = value;
			
			if(value)
			{
				targetToEdit();
			}
		}
		
		/**
		 * 增加行
		 * @param evt 
		 */		
		private function onClickAddColLab(evt : MouseEvent) : void
		{	
			m_dgCols.addItem({text:"New",resizable:"true",sortable:"true"});
			
			refreshToTarget();
		};
		
		/**
		 * 删除行
		 * @param evt 
		 */		
		private function onClickDisColLab(evt : MouseEvent) : void
		{
			if(m_dgCols.selectedIndex >= 0)
			{
				if(m_dpCols.toArray().length > 0)
				{
					m_dpCols.removeItemAt(m_dgCols.selectedIndex);
					refreshToTarget();
				}
			}
		}
		
		/**
		 * 单元项目编辑结束
		 * @param evt 
		 */		
		private function onItemEditEnd(evt : DataGridEvent) : void
		{
			evt.stopPropagation();
			
			if(!m_target) return;
			
			
			refreshToTarget();
		}
		
		/**
		 * 更新目标数据 
		 */		
		private function refreshToTarget() : void
		{
			var lst : Array = m_dpCols.toArray();
			var length : int = lst.length;
			var colums : Array = [];
			
			for(var i:int=0;i<length;i++)
			{
				var dgCol : DataGridColumn = new DataGridColumn;
				var itemData : Object = lst[i];
				
				dgCol.headerText = itemData["text"];
				dgCol.resizable = itemData["resizable"] == "true" ? true : false;
				dgCol.sortable = itemData["sortable"] == "true" ? true : false;
				
				colums.push(dgCol);
			}
			
			if(m_target)
			{
				if(colums.length > 0)
				{
					m_target.columns = colums;
				}
				else
				{
					m_target.removeAllColumns();
				}
			}
		}
		
		/**
		 * 目标转编辑项 
		 */		
		private function targetToEdit() : void
		{
			var columns : Array = m_target.columns;
			var length : int = columns.length;
			var lst : Array = [];
			
			for(var i:int=0;i<length;i++)
			{
				var dgCol : DataGridColumn = columns[i];
				var item : Object = new Object;
				
				item["text"] = dgCol.headerText;
				item["resizable"] = dgCol.resizable;
				item["sortable"] = dgCol.sortable;
				
				lst.push(item);
			}
		
			m_dpCols.removeAll();
			
			if(length > 0)
			{
				m_dpCols.addItems(lst);
			}
		}
	}
}